﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using University.Workshop.Presenters.Square.Modify;

namespace University.Workshop.Presentation.Square.Modify
{
    /// <summary>
    /// Interaction logic for PageFirstStepModifySquare.xaml
    /// </summary>
    public partial class PageFirstStepModifySquare : Page, IFirstStepForModifySquare
    {
        private PresenterModifySquare pInternal;
        private PresenterFirstStepForModifySquare pFirstStepForModifySquare;

        public PageFirstStepModifySquare(PresenterModifySquare pExternal)
        {
            InitializeComponent();
            pInternal = pExternal;
            pFirstStepForModifySquare = new PresenterFirstStepForModifySquare(this);
            Application.Current.MainWindow.Title = "UIRegistrarManzano";
        }

        private void SearchCroquisBtn_Click(object sender, RoutedEventArgs e)
        {
            pFirstStepForModifySquare.LoadImage();
        }

        public string UbicationCroquisSquare
        {
            get
            {
                var op = new OpenFileDialog
                             {
                                 Title = "Buscar Croquis",
                                 Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
                                          "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                                          "Portable Network Graphic (*.png)|*.png"
                             };
                if (op.ShowDialog() == true)
                {
                    return op.InitialDirectory + op.FileName;
                }
                return UbicationCroquisSquareToModify;

            }
            set
            {
                if (!string.IsNullOrWhiteSpace(value))
                    if (File.Exists(value))
                    {
                CroquisUbicationTxBx.Text = value;
                        BitmapImage image = new BitmapImage();
                        using (FileStream stream = File.OpenRead(value))
                        {
                            image.BeginInit();
                            image.StreamSource = stream;
                            image.CacheOption = BitmapCacheOption.OnLoad;
                            image.EndInit(); // load the image from the stream
                        } // close the stream
                        SquareImageImg.Source = image;
                    }
                    else
                    {

                        CroquisUbicationTxBx.Text = string.Empty;
                    }

            }

        }

        public string SquareId
        {
            set
            {
                SquareNameTxBx.Content = value;
            }
        }


        public string CommentSquare
        {
            get { return SquareCommentTxBx.Text; }
            set { SquareCommentTxBx.Text = value; }

        }
        
        public ICollection<Model.Allotment> AllotmentsToModify
        {
            get;
            set;

        }

        public void Update()
        {
            pFirstStepForModifySquare.UpdateData();
        }

        public string UbicationCroquisSquareToModify
        {
            get;
            set;
        }

        public string SystemMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifySystemMessage(value);
            }
        }

        public string ErrorMessage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                pInternal.NotifyErrorMessage(value);
            }
        }

        public void ChargeDataToBasic()
        {
            pFirstStepForModifySquare.ChargeDataMedicineProduct();
        }

        public void GoNextPage()
        {
            pInternal.GoStepTwo();
        }

        public void GoBackPage()
        {

        }

        public Model.Square SquareToModify { get; set; }
    }
}
